package cn.itrip.common;

import java.util.Arrays;

public class HtmlEncode {
    /**
     * &#开头的编码换转成中文
     * @param str
     * @return
     */
    public static String decode(String str) {
        String[] tmp = str.split(";&#|&#|;");
        System.out.println(Arrays.toString(tmp));
        StringBuffer sb = new StringBuffer("");
        for (int i = 0; i < tmp.length; i++) {
            if (tmp[i].matches("\\d{5}")) {
                sb.append((char) Integer.parseInt(tmp[i]));

            } else {
                sb.append(tmp[i]);
            }
        }
        return sb.toString();
    }

    /**
     * 字符串转&#编码
     * @param str
     * @return
     */
    public static String strToDecode(String str) {
        char[] tmp = str.toCharArray();
        System.out.println(Arrays.toString(tmp));
        StringBuffer sb = new StringBuffer("");
        for (int i = 0; i < tmp.length; i++) {
            sb.append("&#").append((int) tmp[i]).append(";");
        }
        return sb.toString();
    }


}
